package com.example.demodb.service;

import com.example.demodb.entity.Song;
import com.example.demodb.entity.User;
import com.example.demodb.entity.UserLove;
import com.example.demodb.entity.UserSong;
import com.example.demodb.repository.ISongRepository;
import com.example.demodb.repository.IUserLoveRepository;
import com.example.demodb.repository.IUserRepository;
import com.example.demodb.repository.IUserSongRepository;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class UserService { //service可以抽象成接口
    @Resource
    private IUserRepository userRepository;
    @Resource
    private ISongRepository songRepository;
    @Resource
    private IUserSongRepository userSongRepository;
    @Resource
    private IUserLoveRepository userLoveRepository;
    public User update(User user){
        return userRepository.save(user);//覆盖
    }
    public void delete(String id){
         userRepository.deleteById(id);
    }
    public List<User> findAll(){
        return userRepository.findAll();
    }
    public String add(User user){
        if(userRepository.findByName(user.getName())==null && userRepository.findById(user.getId()).isEmpty()){
            return userRepository.save(user).getId();
        }
        return "-1";
    }

    public UserSong addUserSong(String userId, String songId, Integer listenCount){
        User user = userRepository.findUserById(userId);
        Song song = songRepository.findSongById(songId);
        UserSong userSong =userSongRepository.findByUserAndSong(user, song);
        if(userSong == null){
            userSong = userSongRepository.save(user.addUserSong(song, listenCount));
        }else{
            Long updateTime = System.currentTimeMillis();
            userSong = userSongRepository.save(user.changeListenCount(userSong,updateTime));
        }
        return userSong;
    }
    
    public UserSong addUserSongLimitByUserSongNum(String userId, String songId, Integer listenCount){
        User user = userRepository.findUserById(userId);
        Song song = songRepository.findSongById(songId);
        UserSong userSong =userSongRepository.findByUserAndSong(user, song);

        Integer userSongsCount = userSongRepository.countByUser(user);
        Integer maxUserSongsCount = 4;
        if(userSong == null && userSongsCount<maxUserSongsCount){
            userSong = userSongRepository.save(user.addUserSong(song, listenCount));

        }else if(userSong == null){//表明userSongs容量超过最大限制，那么就需要删歌，然后再加
            UserSong userSongOldest= userSongRepository.findAllByUserOrderByUpdateTime(user).get(0);
            Long updateTime = System.currentTimeMillis();
            userSong = userSongRepository.save(user.changeUserSong(userSongOldest,song,updateTime));
        }else{
            Long updateTime = System.currentTimeMillis();
            userSong = userSongRepository.save(user.changeListenCount(userSong,updateTime));
        }
        return userSong;
    }

    public List<UserSong> findAllUserSongsByUserOrderByUpdateTimeDesc(String userId){
        User user = userRepository.findUserById(userId);
        return userSongRepository.findAllByUserOrderByUpdateTimeDesc(user);
    }

    public List<UserSong> findAllUserSongsByUserOrderByUpdateTime(String userId){
        User user = userRepository.findUserById(userId);
        return userSongRepository.findAllByUserOrderByUpdateTime(user);
    }



    public User findById(String id) {
        return userRepository.findUserById(id);
    }

    public Set<UserSong> findUserSongsByUserId(String userId) {
        User user = findById(userId);
        return user.getUserSongs();
    }

    public List<String> findUserSongTitleByUserId(String userId) {
        User user = findById(userId);
        Set<UserSong> userSongs = user.getUserSongs();
        List<String> songTitles = new ArrayList<>();
        for (UserSong ele : userSongs) {
            songTitles.add(ele.getSong().getTitle());
        }
        return songTitles;
    }

    public String login(String username, String password) {
        User user = userRepository.findByNameAndPassword(username, password);
        if(user == null){
            return "-1";
        }else{
            return user.getId();
        }
    }

    public UserLove addUserLove(String userId, String songId) {
        User user = userRepository.findUserById(userId);
        Song song = songRepository.findSongById(songId);
        UserLove userLove =userLoveRepository.findByUserAndSong(user, song);
        if(userLove==null){
            return userLoveRepository.save(user.addUserLove(song));
        }
        return userLove;
    }

    public String deleteUserLove(String userId, String songId) {
        User user = userRepository.findUserById(userId);
        Song song = songRepository.findSongById(songId);
        UserLove userLove =userLoveRepository.findByUserAndSong(user, song);
        if(userLove!=null){
            userLoveRepository.delete(userLove);
            return "删除成功";
        }
        return "无法删除不存在的记录";
    }

    @Transactional
    public User updateUserName(String id, String name) {
        userRepository.updateUserName(id,name);
        return userRepository.findUserById(id);
    }
}
